import utils.signals
from django.contrib.auth.models import User
import connectpeople.Constants
from groupevents.models  import Events
import datetime
import calendar
from django.utils.safestring import mark_safe

# The view is used to pull out the flag value
# for email setting
def getEmailFlag(flag_string, curr_email_flag):
    # Split the complete string into two parts
    # based on the required flag string and take second part
    split_flag_string = flag_string.split(curr_email_flag)[1]
    
    # return flag
    return split_flag_string.split(";")[0]
    
# send single email helper. This email is send to single user
def send_single_email_helper(a_sender, a_email, a_name, a_model, a_subject, a_template):
    utils.signals.send_single_mail.send(
                   sender=a_sender, email=a_email, name=a_name, model=a_model, subject=a_subject, template=a_template)
    
# send email helper. This email will be sent to multiple users
def send_email_helper(a_sender, a_email, a_model, a_subject, a_template):
    utils.signals.send_mail.send(
                   sender=a_sender, email=a_email, model=a_model, subject=a_subject, template=a_template)
    
# Get Permission String
def getPermissionString(perm):
    if perm == connectpeople.Constants.owner:
        permission = "Owner"
    elif perm == connectpeople.Constants.board_member:
        permission = "Board Member"
    elif perm == connectpeople.Constants.assist_board_member:
        permission = "Assistant Board Member"
    elif perm == connectpeople.Constants.regular_member:
        permission = "Regular Member"
    else:
        permission = "Non Member"
        
    return permission

# Get User permission
def getUserPermission(mem_grp_data, userid, group_web_address):
    # initial permission as non member
    permission = connectpeople.Constants.non_member
    for each in mem_grp_data:        
        if each.userid == userid and each.group_web_address == group_web_address:
            permission = each.permission
    
    # Return user object
    return permission
        
        
# Get User Event Calendar list
def getUserEventCal(mem_grp_data, year, month):
    group_lst = []
    for each in mem_grp_data:
        group_lst.append(each.group_web_address)
        
    # Get all the events for users groups
    events = Events.objects.filter(group__in=group_lst, event_date__year=year, event_date__month=month)
    
    # initialize calendar
    cal = calendar.Calendar(6)
    # Get all the days in month in week list
    iter = cal.monthdayscalendar(year, month)

    dataHtml= []
    dataHtml.append("<div class='nobreak clearbreak'>")
    for each_week in iter:
        dataHtml.append("<div class='nobreak clearbreak'>") 
        for day in each_week:
            # if day is 0 then it doesn't belong to current month
            if day == 0:
                dataHtml.append("<div class='nobreak cal_day'>&nbsp;</div>")
            # if day lies in event date then status is 1 else 0
            else:
                event_on_day = False
                event = []
                for each_event in events:
                    if each_event.event_date.day == day:
                        event_on_day = True
                        event.append(each_event.id)
                        # save event info in hidden element
                        dataHtml.append("<input type='hidden' id='event" + str(each_event.id) + "' event_name='" + str(each_event.event_name) + "' event_group='"+ str(each_event.group.group_web_address)  + "' group_name='"+ str(each_event.group.group_name) +"'/>");
                      
                if event_on_day == True:
                    dataHtml.append("<div class='nobreak cal_day'> <a event_ids='"+ event.__str__() +"' class='event_day'>" + str(day) + "</a> </div>");
                else:
                    dataHtml.append("<div class='nobreak cal_day'> " + str(day) + " </div>");
        dataHtml.append("</div>");
    dataHtml.append("</div>");
    
    return mark_safe(u''.join(dataHtml))
